package io.gitee.hchaojie.day05;

import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo;

public class MyLinkedList<T> implements LinkedList<T> {
    private Element<T> head = null;

    @Override
    public void add(T s) {
        if (head == null) {
            head = new Element(s, null);
        } else {
            Element tail = head;

            while (tail.next != null) {
                tail = tail.next;
            }

            Element e = new Element(s, null);
            tail.next = e;
        }
    }

    @Override
    public T get(int index) {
        if (index < 0) {
            throw new RuntimeException("下标不能小于0");
        }

        Element<T> node = head;
        for (int i = 1; i <= index; i++) {
            if (node != null) {
                node = node.next;
            } else {
                // index可能 大于 列表长度
                break;
            }
        }

        // node可能为空
        return node == null ? null : node.data;
    }

    /**
     * @param index - 插入的元素的位置，0 <= index <= list.size()
     * @param s
     */
    @Override
    public void add(int index, T s) {
    }

    @Override
    public void remove(T s) {

    }

    @Override
    public void remove(int index) {
    }

    @Override
    public int size() {
        Element node = head;

        int count = 0;
        while (node != null) {
            node = node.next;
            count++;
        }

        return count;
    }

    static class Element<E> {
        private E data;
        private Element next;

        public Element(E data, Element<E> next) {
            this.data = data;
            this.next = next;
        }
    }
}
